package morncloud.com.notes.services;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import morncloud.com.notes.beans.Constants;
import morncloud.com.notes.database.NoteDataBaseOpenHelper;
import morncloud.com.notes.database.Provider;

public class NoteContentProvider extends ContentProvider {
    private SQLiteOpenHelper mOpenHelper;
    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int uri_note = 1;

    static {
        sUriMatcher.addURI(Constants.AUTHOURIES, Constants.NOTE, uri_note);
    }

    public NoteContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code = sUriMatcher.match(uri);
        SQLiteDatabase database = null;
        int result = 0;
        try {
            if (code == UriMatcher.NO_MATCH) {
                return 0;
            } else {
                database = mOpenHelper.getWritableDatabase();
                database.beginTransaction();
                switch (code) {
                    case uri_note: {
                        Log.e("print",selectionArgs.toString());
                        /**
                         * 删除有问题
                         */
                        result = database.delete(Provider.TABLE_NOTES, selection, selectionArgs);
                        break;
                    }
                    default: {
                        result = 0;
                        break;
                    }
                }
                database.setTransactionSuccessful();
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = 0;
        } finally {
            if (database != null) {
                database.endTransaction();
            }
        }
        return result;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        int code = sUriMatcher.match(uri);
        SQLiteDatabase database = null;
        try {
            if (code == UriMatcher.NO_MATCH) {
                return null;
            } else {
                //lazy loading
                database = mOpenHelper.getWritableDatabase();
                //begin transaction
                database.beginTransaction();
                long result = 0;
                switch (code) {
                    case uri_note: {
                        result = database.insert(Provider.TABLE_NOTES, null, values);
                        break;
                    }
                    default: {
                        break;
                    }
                }
                database.setTransactionSuccessful();
                if (result > 0) {
                    return ContentUris.withAppendedId(uri, result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (database != null) {
                database.endTransaction();
            }
        }
        return null;
    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new NoteDataBaseOpenHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int code = sUriMatcher.match(uri);
        //lazy load
        SQLiteDatabase database = null;
        Cursor cursor = null;
        try {
            if (code == UriMatcher.NO_MATCH) {
                return null;
            } else {
                // match
                database = mOpenHelper.getReadableDatabase();
                switch (code) {
                    case uri_note: {
                        cursor = database.query(Provider.TABLE_NOTES, projection, selection, selectionArgs, null, null, sortOrder);
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            cursor = null;
        }
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int code = sUriMatcher.match(uri);
        int result = 0;
        SQLiteDatabase database = null;
        try {
            if (code == UriMatcher.NO_MATCH) {
                return 0;
            } else {
                database = mOpenHelper.getWritableDatabase();
                database.beginTransaction();
                switch (code) {
                    case uri_note: {
                        result = database.update(Provider.TABLE_NOTES, values, selection, selectionArgs);
                        break;
                    }
                    default: {
                        result = 0;
                        break;
                    }
                }
                database.setTransactionSuccessful();
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = 0;
        } finally {
            if (database != null) {
                database.endTransaction();
            }
        }
        return result;
    }
}
